
// button object
local PANEL = {};


// materials
local matLogo = Material( "musex/musex" );
local matGradient = Material( "gui/gradient" );


/*------------------------------------
	initialize the splash screen
------------------------------------*/
function PANEL:Init( )

	// sounds
	util.PrecacheSound( "musex/musex.wav" );

	// disable animation (for now)
	self.Animate = false;
	
	// custom derma hooks
	self.AnimIn = Derma_Anim( "OpenAnim", self, self.OpenAnim );

end


/*------------------------------------
	open the main window
------------------------------------*/
function PANEL:Open( )

	// ignore if we're already visible
	if ( self:IsVisible() ) then
	
		// blarg
		return;
		
	end
	
	// make me visible
	self:SetVisible( true );
	
	// disable keyboard input
	self:SetKeyboardInputEnabled( false );
	
	// enable mouse input
	self:SetMouseInputEnabled( false );
	
	// start the open animation
	self.AnimIn:Start( 0.1 );

end


/*------------------------------------
	handle the opening animation
------------------------------------*/
function PANEL:OpenAnim( anim, delta, data )

	// check if it has been started already
	if ( anim.Started ) then
	
		// starting alpha
		self:SetAlpha( 0 );
		
	end
	
	// see if it has finished
	if ( anim.Finished ) then
	
		// fully visible
		self:SetAlpha( 255 );
		
		// start animation
		self.Animate = true;
		self.AnimAngle = 0;
		self.AnimTime = 0;
		self.AnimCount = 0;
		
		// all done
		return;
		
	end
	
	// animate the alpha
	self:SetAlpha( 255 * delta );

end


/*------------------------------------
	splash screen thinkage
------------------------------------*/
function PANEL:Think( )
	
	// run the animation functions
	self.AnimIn:Run();

end


/*------------------------------------
	splash paint
------------------------------------*/
function PANEL:Paint( )

	// check if its time to draw the animation
	if ( self.Animate ) then
	
		// see if we should move the animation
		if ( MXCurTime() > self.AnimTime ) then
		
			// animation rotation speed
			local speed = 10;
			
			// set the next movement time
			self.AnimTime = MXCurTime() + ( FrameTime() * 2 );
			
			// animate the angle
			self.AnimAngle = ( self.AnimAngle <= ( 360 - speed ) ) && self.AnimAngle + speed || 0;
			
			// check if we've done a loop
			if ( self.AnimAngle == 0 ) then
			
				// increase the loop count
				self.AnimCount = self.AnimCount + 1;
				
				// check if we've hit the sound time
				if ( self.AnimCount == 1 ) then
				
					// play it
					surface.PlaySound( "musex/musex.wav" );
				
				// determine if we've hit our max loop count
				elseif ( self.AnimCount >= 2 ) then
				
					// make sure the vgui hasn't been killed (somehow?)
					if ( MuseX.VGUIValid() ) then
					
						// open the main vgui
						MuseX.VGUI:Open();
						
					end
					
					// update splash state
					MuseX.Splash = 2;
					
					// destroy us
					self:Remove();
					
					// bai fool
					return;
				
				end
			
			end
			
		end
		
		// calculate center positions
		local cx = self:GetWide() * 0.5;
		local cy = self:GetTall() * 0.5;
		
		// distance away from center
		local dist = ( self:GetWide() * 0.5 ) * 0.5;
		
		// radius size of pie wedge
		local radius = 90;
		
		// loop the entire size
		for i = 0, radius, 2.5 do
		
			// calculate the alpha
			local alpha = 200 - ( i * 2 );
			
			// calculate the center position of the rect
			local drawx = cx + math.cos( math.rad( self.AnimAngle - i ) ) * dist;
			local drawy = cy + math.sin( math.rad( self.AnimAngle - i ) ) * dist;
			
			// draw the rect
			surface.SetMaterial( matGradient );
			surface.SetDrawColor( 0, 0, 0, alpha );
			surface.DrawTexturedRectRotated( drawx, drawy, 20, dist * 2, ( 360 - ( self.AnimAngle - i ) ) + 90 );
			
		end
	
	end
	
	// draw the logo
	surface.SetMaterial( matLogo );
	surface.SetDrawColor( 255, 255, 255, 255 );
	surface.DrawTexturedRect( 0, 0, self:GetWide(), self:GetTall() );
	
	// ...annnnnnnd we're done
	return false;

end

// define
derma.DefineControl( "MXSplash", "", PANEL, "DPanel" );
